<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>Couchdbkit - Listen to database changes</title>
    <!--[if IE]>

    <script>
        // allow IE to recognize HTMl5 elements
        document.createElement('section');
        document.createElement('article');
        document.createElement('aside');
        document.createElement('footer');
        document.createElement('header');
        document.createElement('nav');
        document.createElement('time');

    </script>
    <![endif]-->

    <link rel="stylesheet" href="/css/couchdbkit.css?20090615" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="/static/css/print.css?20090615" type="text/css" media="print" />
    <script type="text/javascript" src="/js/prettify.js"></script>
    
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/feed.xml" />
    

    
        
</head>
<body onload="prettyPrint()">
    
    <div class="container">
        <header id="site_header">
            <h1><a href="/"><span>couchdbkit</span></a></h1>
            <ul id="main_nav">
                <li><a href="/">home</a></li>
                <li><a href="/blog/">news</a></li>
                <li><a href="/download.html">download</a></li>
                <li><a href="/docs/">documentation</a></li>
                <li><a href="/docs/api/">API</a></li>
            </ul>

        </header>
        
        
        
        
        <aside id="sidebar">
            <ul class="sidenav">
                <li><a href="/blog/">Latest news</a></li>
                <li><a href="/download.html">Download latest</a></li>
                <li><a href="/docs/gettingstarted.html">Getting started</a></li>
                <li><a href="/docs/faq.html">Frequently Asked Questions</a></li>
                <li><a href="/docs/about.html">About couchdbkit</a></li>
                <li><a href="/contact.html">Getting help with couchdbkit</a></li>
            </ul>
            <div class="info">
                <h3>Getting started</h3>
                <p>See the <a href="/download.html">download instructions</a> for information on getting couchdbkit, and read the <a href="/docs/gettingstarted.html">getting started</a> instructions to start programming CouchDB in your own application in less than 10 minutes.</p>
            </div>
            <div class="info">
                <h3>Get involved</h3>
                <ul>
                    <li><p><a href="http://webchat.freenode.net/?channels=couchdbkit">#couchdbkit IRC channel</a>: chat with other couchdbkit users</p></li>
                    <li><p><a href="http://github.com/benoitc/couchdbkit/issues">Ticket system</a>: report bugs and make feature requests</p></li>
                    <li><p><a href="http://groups.google.com/group/couchdbkit">Mailing-List</a>: The main list for help and follow changes.</p></li>
                </ul>
                        
            </div>
           
        </aside>
        
    
        
<section id="content">
    <article>
        <h1>Listen to database changes</h1>
        	<p>couchdbkit 0.6.0 introduced a new <span class="caps">API</span> to listen for CouchDB changes.</p>

	<p>The `couchdbkit.changes` modules allows you to listen for changes with a<br />streaming <span class="caps">API</span>.</p>

	<h2>Stream changes in your application:</h2>

	<p>To listen for change, instantiate the `couchdbkit.changes.ChangesStream`<br />object and iterrate it:</p>

	<p><pre class="code prettyprint">
    from couchdbkit import Server
    from couchdbkit.changes import ChangesStream, fold, foreach</p>

    s = Server()
    db = s['testdb&#8217;]
    stream = ChangesStream(db, feed=&#8220;continuous&#8221;, heartbeat=True)

    print &#8220;got change now&#8221;
    for change in stream:
        print change
</pre>

	<p>You can also use it as a context:</p>

	<p><pre class="code prettyprint">
    from couchdbkit import Server
    from couchdbkit.changes import ChangesStream, fold, foreach</p>

    s = Server()
    db = s['testdb&#8217;]
    with ChangesStream(db, feed=&#8220;continuous&#8221;, heartbeat=True) as stream:
        for change in stream:
            print change
</pre>

	<p>Note: if you want to use it with gevent, you can just setup a gevent<br />pool:</p>

	<p><pre class="code prettyprint">
    from couchdbkit import Server
    from couchdbkit.changes import ChangesStream, fold, foreach</p>

    from restkit.conn import Connection
    from socketpool.pool import ConnectionPool

    pool = ConnectionPool(factory=Connection, backend=&#8220;gevent&#8221;)

    s = Server(pool=pool)
    db = s['testdb&#8217;]
    with ChangesStream(db, feed=&#8220;continuous&#8221;, heartbeat=True) as stream:
        for change in stream:
            print change
</pre>

	<p>For more information about creating a greened pool, read the <a<br />href=&#8220;http://benoitc.github.com/restkit/green.html&#8221;>restkit<br />documentation</a>.</p>

	<p>You can of course spawn or do anything you want with the gevent or<br />eventlet libraries.</p>

	<h2>fold changes:</h2>

	<p>`couchdbkit.changes.fold` allows you to fold all changes and pass<br />the change to a function, while collecting results to an accumularor.<br />The accumulator is returned at the end of the changes.</p>

	<p>Exemple to fetch all changes in a streaming fashion:</p>

	<p><pre class="code prettyprint">
    def fold_fun(c, acc):
        acc.append&#169;
        return acc</p>

    acc = fold(db, fold_fun, [])
</pre>

	<p>Note: The function take an optionnal `since` parameter.</p>

	<h2>Iterrate all changes</h2>

	<p>`couchdbkit.changes.foreach` allows you to itterate all changes and pass<br />the change to a function:</p>

	<p><pre class="code prettyprint">
    def print_change&#169;:
        print c</p>

    foreach(db, print_change)
</pre>

	<p>Note: The function take an optionnal `since` parameter.</p>
    </article>
</section>

        
        <footer id="bottom">
            <p class="copyright">2008-2012 &copy; <a href="http://benoitc.org">Benoît Chesneau</a> - Some rights reserved.</p>
            <div id="cc"><a rel="license" href="http://creativecommons.org/licenses/by/2.0/fr/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/2.0/fr/80x15.png" /></a> This <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/InteractiveResource" rel="dc:type">website</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/2.0/fr/">Creative Commons Attribution 2.0 France License</a>.</div>
            <p>Hosted on <a href="http://github.com/">GitHub</a></p>
        </footer>
        
    </div>

    
    
    
</body>
</html>